package cn.zn.monitor.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.zn.base.common.constant.Constants;
import cn.zn.framework.bean.LoginUser;
import cn.zn.monitor.dto.UserOnlineDto;
import cn.zn.monitor.service.UserOnlineService;
import cn.zn.monitor.vo.UserOnlinePageVo;
import cn.zn.redis.service.RedisService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class UserOnlineServiceImpl implements UserOnlineService {

    @Autowired
    private RedisService redisService;

    @Override
    public Object selectUserOnlineList(UserOnlinePageVo vo) {
        Collection<String> keys = redisService.keys(Constants.LOGIN_TOKEN_KEY + "*");
        List<UserOnlineDto> userOnlines = new ArrayList<>();
        for (String key : keys) {
            LoginUser user = redisService.getCacheObject(key);
            UserOnlineDto userOnline = loginUserToUserOnline(user, vo);
            if (userOnline != null) {
                userOnlines.add(userOnline);
            }
        }

        if (!vo.getPage()) {
            return userOnlines;
        }

        IPage<UserOnlineDto> page = new Page<>(vo.getCurrent(), vo.getSize(), userOnlines.size());
        int start = (vo.getCurrent() - 1) * vo.getSize();
        int end = Math.min(vo.getCurrent() * vo.getSize(), userOnlines.size());
        page.setRecords(userOnlines.subList(start, end));
        return page;
    }

    @Override
    public void forceLogout(String tokenId) {
        redisService.deleteObject(Constants.LOGIN_TOKEN_KEY + tokenId);
    }

    /**
     * 设置在线用户信息
     *
     * @param user 用户信息
     * @return 在线用户
     */
    private UserOnlineDto loginUserToUserOnline(LoginUser user, UserOnlinePageVo vo) {
        if (ObjectUtil.isNull(user) || ObjectUtil.isNull(user.getTheUser())) {
            return null;
        }

        boolean add = true;
        // 如果有查询条件
        if (StrUtil.isNotBlank(vo.getLoginName()) && !user.getUsername().contains(vo.getLoginName())) {
            // 登录名不一致，不添加
            add = false;
        }
        if (StrUtil.isNotBlank(vo.getUserName()) && !user.getTheUser().getUserName().contains(vo.getUserName())) {
            add = false;
        }
        if (!add) {
            return null;
        }

        UserOnlineDto userOnline = new UserOnlineDto();
        userOnline.setTokenId(user.getToken());
        userOnline.setLoginName(user.getUsername());
        userOnline.setUserName(user.getTheUser().getUserName());
        userOnline.setIpAddress(user.getIpAddress());
        userOnline.setBrowser(user.getBrowser());
        userOnline.setOs(user.getOs());
        userOnline.setLoginTime(new Date(user.getLoginTime()));
        userOnline.setDeptName(user.getTheUser().getDeptName());
        return userOnline;
    }

}
